<template>
  <div class="wrap">
    <div class="detail-top">
      <div class="detail">
        <!-- <div style="padding: 20px 0"></div> -->
        <cumb :navs="navs" />
        <div class="flex">
          <div class="imgs">
            <img :src="fixSrc(productInfo.coverImg)" class="big" alt="" />
            <ul>
              <li
                :class="{ active: acitveBigIndex === index }"
                v-for="(item, index) in productInfo.imgAltas"
                :key="index"
                @mouseenter="changeBig(index)"
              >
                <img :src="fixSrc(item.src)" alt="" />
              </li>
              <!-- <li>
                <img src="../assets/img/userImg.f8bbec5e.png" alt="" />
              </li>
              <li>
                <img src="../assets/img/userImg.f8bbec5e.png" alt="" />
              </li>
              <li>
                <img src="../assets/img/userImg.f8bbec5e.png" alt="" />
              </li> -->
            </ul>
          </div>
          <div class="detail-text">
            <h4>{{ productInfo.name }}</h4>
            <p class="intro">
              {{ productInfo.seriesSubTitle }}
            </p>
            <div class="money">{{ productInfo.coin }}积分</div>
            <p style="padding-bottom: 20px">
              {{ productInfo.parameterJson[0].title }}
            </p>
            <ul class="category">
              <li
                v-for="item in productInfo.parameterJson[0].parameters"
                :key="item.id"
                :class="{ active: item.id == $route.params.id }"
                @click="$router.push(`/detil/${item.id}`)"
              >
                {{ item.title }}
              </li>
            </ul>
            <div class="count">
              <p>数量</p>
              <div class="flex">
                <span class="add" @click="add">+</span>
                <input type="text" v-model.number="count" />
                <span class="sub" @click="subtrct">-</span>
              </div>
            </div>
            <div class="addto">
              <div class="btn" @click="cart">加入购物车</div>
              <div class="btn">立即购买</div>
            </div>
          </div>
        </div>
      </div>
      <div class="canbuy">
        <h1>你还可以兑换</h1>
        <ul class="right" v-for="(item, index) in themYouCanBuy" :key="index">
          <li>
            <img :src="fixSrc(item.img)" alt="" />
          </li>
          <li>
            <div class="one">{{ item.name }}</div>
            <div class="two">
              <span>{{ item.coin }}</span>
              <img
                style="width: 30px; height: 30px"
                src="@/assets/img/monad.png"
                alt=""
              />
            </div>
          </li>
        </ul>
      </div>
    </div>
    <!-- 下面的tab ， 产品详情+常见问题 -->
    <div>
      <div class="tabs">
        <div class="tab" :class="{ active: show }" @click="show = true">
          商品详情
        </div>
        <div :class="{ active: !show }" class="tab" @click="show = false">
          常见问题
        </div>
      </div>
      <div class="contents">
        <div
          v-show="show"
          class="detail show"
          v-html="productInfo.description"
        ></div>
        <div class="qa" v-show="!show">
          <h5>兑换后运费如何收取？</h5>
          <p>
            商城内大部分实物产品均可包邮，也支持上门自提。虚拟产品则无需运费，可在兑换后直接使用。
          </p>
          <p>
            （注：新疆，西藏，内蒙古，青海，海南，宁夏暂不支持包邮，在此地区兑换商品需提前与客服确认运费。）
          </p>
          <h5>如何申请退换货？</h5>
          <p>所有产品均为鸡腿兑换礼品，无质量问题不支持退换货；</p>
          <p>如因喜好不同，或到货后后悔等原因，不支持退货；</p>
          <p>
            因客户原因如联系方式有误、电话停机、不接电话、无理由拒收等造成快件无法正常签收的情况，叩丁狼均不进行补发或赔付；
          </p>
          <p>
            如因质量问题需要退货，将由客服人员与客户联系，确认邮寄地址与流程。退回物品不得有人为损坏或使用痕迹，否则拒绝退换；
          </p>
          <p>
            如顾客选择自行寄回商品，请先垫付运费，到货验证商品后，叩丁狼将以鸡腿发放形式为用户报销运费，不接受单方面到付件。
          </p>
          <h5>如何开具发票？</h5>
          <p>
            叩丁狼积分商城产品均为定制礼品，使用积分兑换的产品均不支持开票。
          </p>
          <h5>关于鸡腿价格问题</h5>
          <p>
            因可能存在系统缓存、页面更新延迟等不确定性情况，导致价格显示异常，商品具体售价请以订单结算页价格为准。如您发现异常情况出现，请立即联系我们补正，以便您能顺兑换。
          </p>
          <p>联系我们 www.wolfcode.cn 微信公众号搜索“叩丁狼”</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getproductidsapi, getaddapi, getuserapi } from "@/request/api";
import { mapMutations } from "vuex";
import { fixSrc } from "@/untis";
import cumb from "@/components/cumb.vue";
import { saveCountKey } from "@/request/constant";
export default {
  data() {
    return {
      navs: [
        { text: "首页", path: "/home" },
        { text: "全部商品", path: "/goods" },
        { text: "商品详情", path: "/detil" },
      ],
      shops: 0,
      count: 1,
      acitveBigIndex: 0,
      show: true,
      productInfo: {
        coverImg: "",
        description: "",
        imgAltas: [],
        name: "",
        seriesSubTitle: "",
        coin: "",
        parameterJson: [{}],
        stock: 0,
      },
      themYouCanBuy: {},
    };
  },
  created() {
    //   要通过路由跳转的参数得到当前商品的id
    // console.log(this.$route.params.id);
    this.getDetail();
    this.getCount();
  },
  methods: {
    ...mapMutations({
      cartchange: "user/cartchange",
      updateUser: "user/updateUserInfo",
    }),

    getDetail() {
      getproductidsapi(this.$route.params.id).then((res) => {
        if (res.code === 0) {
          this.productInfo = res.data.productInfo;
          // console.log(res.data.productInfo.stock);
          this.themYouCanBuy = res.data.themYouCanBuy;
          this.productInfo.description =
            res.data.productInfo.description.replace(
              /upload\/images\//g,
              "https://sc.wolfcode.cn/upload/images/"
            );
        }
      });
    },
    // 图片绑定
    changeBig(index) {
      this.acitveBigIndex = index;
      this.productInfo.coverImg =
        this.productInfo.imgAltas[this.acitveBigIndex].src;
    },
    fixSrc(src) {
      return fixSrc(src);
    },
    add() {
      if (this.count >= this.productInfo.stock) {
        // 不能超过最大的值
        return;
      }
      this.count++;
      const id = parseInt(this.$route.params.id);
      this.save(id, this.count);
    },
    subtrct() {
      if (this.count <= 1) {
        return;
      }
      this.count--;
      const id = parseInt(this.$route.params.id);
      this.save(id, this.count);
    },
    save(id, value) {
      // 首先存储本地数据读取
      let data = localStorage.getItem(saveCountKey);
      if (data) {
        // 判断data有没有值，有的话就找本地存起来装换成对象，没有的话就简历一个对象
        data = JSON.parse(data);
      } else {
        data = {};
      }
      // 更新数据，如果要累加，需要判断是否存在，如果只是覆盖就不用判断
      data[id] = value;
      // 写入缓存
      localStorage.setItem(saveCountKey, JSON.stringify(data));
    },
    getCount() {
      let data = localStorage.getItem(saveCountKey);
      if (data) {
        data = JSON.parse(data);
        // 之前不一定有存过当前的件数
        const id = this.$route.params.id;
        if (Object.keys(data).includes(id)) {
          this.count = data[id];
        }
      }
    },
    cart() {
      // 添加购物车数据
      if (!this.$store.state.shopshow) {
        this.$store.dispatch("asynctoasts", {
          toastmsg: "登录一下",
          toastIcon: "warning",
        });
        return;
      }
      // 添加成功之后
      getaddapi({
        productId: this.$route.params.id,
        total: this.count,
      })
        .then((res) => {
          if (res.code === 0) {
            this.$store.dispatch("asynctoasts", {
              toastmsg: "添加成功",
              toastIcon: "success",
            });
            return getuserapi();
            // 解决地狱回调
          }
        })
        .then((res) => {
          if (res.code === 0) {
            // console.log(res.data.cartTotal);
            this.shops = res.data.cartTotal;
            this.cartchange({ cartTotal: this.shops });
          }
        });
    },
  },
  watch: {
    "$route.params": function () {
      this.getDetail();
    },
  },
  components: {
    cumb,
  },
};
</script>

<style lang="less" scoped>
.detail-top {
  display: flex;
  .detail {
    width: 970px;
    border-right: 1px solid #000;
    .flex {
      display: flex;
      .imgs {
        .big {
          width: 460px;
          height: 440px;
          margin-bottom: 20px;
        }

        ul {
          display: flex;
          justify-content: space-between;
          li {
            width: 100px;
            height: 100px;
            cursor: pointer;

            opacity: 0.5;
            &.active {
              opacity: 1;
            }

            img {
              width: 100%;
            }
          }
        }
      }
      .detail-text {
        padding-left: 40px;
        h4 {
          height: 27px;
          font-size: 28px;
          font-family: SourceHanSansSC;
          font-weight: 400;
          color: #000000;
          line-height: 23px;
        }
        .money {
          font-size: 28px;
          font-family: SourceHanSansSC;
          font-weight: bold;
          color: #fd604d;
          line-height: 23px;
          padding: 40px 0;
        }
        .intro {
          width: 430px;
          height: 35px;
          font-size: 14px;
          font-family: SourceHanSansSC;
          font-weight: 300;
          color: #999999;
          line-height: 20px;
          padding-top: 20px;
        }
        .category {
          display: flex;
          flex-wrap: wrap;
          li {
            // width: 76px;
            height: 32px;
            text-align: center;
            line-height: 32px;
            border: 1px solid #d1d1d1;
            margin-right: 20px;
            margin-bottom: 20px;
            cursor: pointer;
            &.active {
              // width: 76px;
              height: 32px;
              border: 1px solid #0a328e;
              color: #0a328e;
            }
          }
        }
        .count {
          p {
            font-size: 16px;
            font-family: SourceHanSansSC;
            font-weight: 300;
            color: #000000;
            line-height: 23px;
            padding: 20px 0;
          }
          .flex {
            display: flex;
            .add,
            .sub {
              width: 30px;
              height: 32px;
              text-align: center;
              line-height: 32px;
              border: 1px solid #d1d1d1;
              color: #d1d1d1;
              cursor: pointer;
            }
            input {
              width: 76px;
              height: 32px;
              border: 1px solid #d1d1d1;
              border-left: 0;
              border-right: 0;
              text-align: center;
            }
          }
        }
        .addto {
          display: flex;
          padding-top: 20px;
          .btn {
            width: 150px;
            height: 46px;
            text-align: center;
            line-height: 46px;
            margin-right: 20px;
            color: #fff;
            &:nth-child(1) {
              background: #3dc36b;
            }
            &:nth-child(2) {
              background: #fd604d;
            }
          }
        }
      }
    }
  }
  .canbuy {
    width: 211px;
    h1 {
      margin-top: 50px;
      margin-bottom: 20px;
    }
    .right {
      height: 80px;
      display: flex;
      margin-bottom: 20px;
      // justify-content: space-between;
      li {
        flex-wrap: nowrap;
        margin-bottom: 20px;

        img {
          vertical-align: middle;
          width: 80px;
          height: 80px;
        }
        &:nth-child(2) {
          margin-left: 15px;
        }
        .two {
          margin-top: 10px;
        }
      }
      .one {
        width: 110px;
        text-overflow: ellipsis;
        overflow: hidden;
        white-space: nowrap;
      }
    }
  }
}
.tabs {
  border-top: 1px solid #ececec;
  display: flex;
  .tab {
    width: 120px;
    height: 50px;
    text-align: center;
    line-height: 50px;
    font-size: 18px;
    font-family: SourceHanSansSC;
    font-weight: bold;
    color: #333333;
    cursor: pointer;
    &.active {
      background: #ececec;
    }
  }
}

.contents {
  .detail {
    // display: none;
  }
  .qa {
    h5 {
      font-size: 16px;
      font-weight: bold;
      line-height: 36px;
    }
    p {
      line-height: 30px;
      color: #ccc;
    }
  }
  .show {
    display: block;
  }
}
</style>